package com.xyg.exception;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;

import java.util.Date;

@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler( Exception.class )
    public ResponseEntity< ? > globalExceptionHandler ( Exception ex, WebRequest request ) {
        ErrorDetails errorDetails = new ErrorDetails( new Date(), ex.getMessage(),
                request.getDescription( false ) );
        return new ResponseEntity<>( errorDetails, HttpStatus.INTERNAL_SERVER_ERROR );
    }
    
    @ExceptionHandler( NotLoggedInException.class )
    public ResponseEntity< ErrorResponse > handleNotLoggedInException ( NotLoggedInException ex ) {
        ErrorResponse errorResponse = new ErrorResponse( ex.getHttpStatus().value(), ex.getMessage() );
        return new ResponseEntity<>( errorResponse, ex.getHttpStatus() );
    }

}